package com.hzhk.happyrent.ui.adapter.viewpager;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.bean.rent.RentHeaderPagerBean.DataBean.SlidersBean;
import com.hzhk.happyrent.ui.activity.rent.ProductDetailsActivity;
import com.hzhk.happyrent.utils.ListUtils;
import com.hzhk.happyrent.utils.glide.ShowImageUtils;

import java.util.List;

/**
 * Created by hzhk on 2016/6/8.
 */
public class FocusImageAdapter extends PagerAdapter implements
		View.OnClickListener {

	private Context mContext;
	private List<SlidersBean> url;

	public FocusImageAdapter(Context context, List<SlidersBean> url) {
		this.mContext = context;
		this.url = url;
	}

	@Override
	public int getCount() {
		if(!ListUtils.isEmpty(url)){
			return Integer.MAX_VALUE/2;
		}else{
			return 0;
		}
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public Object instantiateItem(final ViewGroup container, int position) {

		if (url.size() == 0) {
			return null;
		}
		position = position % url.size();
		ImageView imageView = new ImageView(mContext);
		imageView.setId(position);
		imageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				int tag=view.getId();
				if(!TextUtils.isEmpty(url.get(tag).getType())&&"2".equals(url.get(tag).getType())){
					Intent intent=new Intent(mContext, ProductDetailsActivity.class);
					intent.putExtra(Constants.GOODS_ID,url.get(tag).getGoodsId());
					mContext.startActivity(intent);
				}
			}
		});
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		ShowImageUtils.showImageViewToCircleNoPlaceHolder(mContext,(url.get(position)).getAttach(),imageView,30);
		imageView.setTag(position);
		ViewParent parent = imageView.getParent();
		if (parent != null) {
			ViewGroup viewGroup = (ViewGroup) parent;
			viewGroup.removeView(imageView);
		}

		container.addView(imageView);
		return imageView;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
		object = null;
	}

	@Override
	public void onClick(View v) {

	}
}
